//删除audit plan时同时删除audit plan下的audit记录
trigger deleteRelatedAudit  on Audit_Plan__c (before delete) {
    list<id> auditPlanIDList=new list<id>();
    for(audit_plan__c t:trigger.old){
        if(t.Audit_Date__c<=date.today()){
            t.addError('该记录不能被删除!');
        }else{
            auditPlanIDList.add(t.id);
        }
    }
    
    if(auditPlanIdList.size()>0){
        list<audit__c> auditList=[select id from audit__c where audit_plan__c in: auditPlanIDList];
        if(auditList.size()>0){
            delete auditList;
        }
    }
    
}